//! Driver 测试

use crate::driver::keyboard::{key_code::KeyCode, KeyboardDriver, KeyboardDriverFunctions};

/// 连续按下然后松开按键
///
/// 为了方便测试，等待 3 秒再实际开始按键。
/// 每次按键之间间隔 1 秒，按下与松开之间间隔 1 秒。
#[test]
fn press_keys() {
    // 按键序列
    let key_codes = [
        KeyCode::KeyA,
        KeyCode::Digit0,
        KeyCode::Numpad0,
        KeyCode::Comma,
        KeyCode::F1,
    ];

    // 键盘驱动
    let driver = KeyboardDriver::default();

    // 等待 3 秒
    println!("等待 3 秒...");
    std::thread::sleep(std::time::Duration::from_secs(3));

    // 按键测试
    for key_code in key_codes {
        driver.key_down(key_code);
        println!("按下按键 {key_code:?}");
        std::thread::sleep(std::time::Duration::from_millis(1000));
        driver.key_up(key_code);
    }
}

/// [`KeyCode`] 的序列化与反序列化测试
#[test]
fn serde_key_code() {
    // 1. 序列化单个 [`KeyCode`]
    let key_code = KeyCode::KeyA;
    let serialized = serde_json::to_string(&key_code).unwrap();
    assert_eq!(serialized, "\"KeyA\"");

    // 2. 反序列化单个 [`KeyCode`]
    let deserialized: KeyCode = serde_json::from_str(&serialized).unwrap();
    assert_eq!(key_code, deserialized);

    // 3. 序列化多个 [`KeyCode`]
    let key_codes = vec![KeyCode::KeyA, KeyCode::KeyB, KeyCode::KeyC];
    let serialized = serde_json::to_string(&key_codes).unwrap();
    assert_eq!(serialized, "[\"KeyA\",\"KeyB\",\"KeyC\"]");

    // 4. 反序列化多个 [`KeyCode`]
    let deserialized: Vec<KeyCode> = serde_json::from_str(&serialized).unwrap();
    assert_eq!(key_codes, deserialized);
}
